﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using FDSimpleModelGenerator.Entities;
using FDSimpleModelGenerator.IRepositories;
using FDSimpleModelGenerator.IServices;
using FDSimpleModelGenerator.Repositories;

namespace FDSimpleModelGenerator.Services
{
    /// <summary>
    /// 模板类服务
    /// </summary>
    public class BasicService<T> : IBasicService<T> where T : Entity, new()
    {
        private readonly IBasicRepository<T> _basicRepository;

        public BasicService(IBasicRepository<T> basicRepository)
        {
            _basicRepository = basicRepository;
        }

        /// <summary>
        /// 获取子类对应仓储
        /// </summary>
        /// <typeparam name="PT"></typeparam>
        /// <returns></returns>
        public PT Repository<PT>()
        {
            return (PT)_basicRepository;
        }

        public T Get(int id)
        {
            return _basicRepository.Get(id);
        }

        public IList<T> GetList()
        {
            return _basicRepository.GetList();
        }

        public bool Add(T classTemplate)
        {
            return _basicRepository.Add(classTemplate);
        }

        public bool Update(T classTemplate)
        {
            return _basicRepository.Update(classTemplate);
        }

        public bool AddOrUpdate(T classTemplate)
        {
            return _basicRepository.AddOrUpdate(classTemplate);
        }

        public bool Delete(int id)
        {
            return _basicRepository.Delete(id);
        }

        #region 异步操作
        public async Task<T> GetAsync(int id)
        {
            return await _basicRepository.GetAsync(id);
        }

        public async Task<IList<T>> GetListAsync()
        {
            return await _basicRepository.GetListAsync();
        }

        public async Task<bool> AddAsync(T classTemplate)
        {
            return await _basicRepository.AddAsync(classTemplate);
        }

        public async Task<bool> UpdateAsync(T classTemplate)
        {
            return await _basicRepository.UpdateAsync(classTemplate);
        }

        public async Task<bool> AddOrUpdateAsync(T classTemplate)
        {
            return await _basicRepository.AddOrUpdateAsync(classTemplate);
        }

        public async Task<bool> DeleteAsync(int id)
        {
            return await _basicRepository.DeleteAsync(id);
        }

        #endregion
    }
}
